<?php
/**
 * Celebrio Database module
 * 
 * @copyright  Copyright (c) 2011 Celebrio Software
 * @package    Celebrio
 * @subpackage Database
 */
namespace Celebrio\Database;

use \dibi;

use Celebrio\Core\IModuleInit;
use Celebrio\Database\DatabaseInit;

use Celebrio\AccessControl\AclConstants;

use Celebrio\PasswordHasher;

/**
 * MySQL initialization/installation routines.
 *
 * @author     Albireo Solutions
 * @package    Kernel
 */
class MySQLInit extends DatabaseInit implements IDatabaseInit {
    
    public function init(array $settings = array()) {
        ini_set("mysql.connect_timeout","5");
        $settings["result"]["detectTypes"] = true;
        parent::init($settings);
    }

    public function install(array $configuration = array()) {
        ini_set("mysql.connect_timeout","5");
        parent::installDatabase($configuration);
    }
    
    public function checkFunctionality($values) {
        ini_set("mysql.connect_timeout","5");
        parent::checkFunctionality($values);
    }
    
    public function isAvailable() {
        if (extension_loaded("mysql")) {
            return true;
        } else {
            return false;
        }
    }
    
    public function changeConfig(array $configuration) {
        return $configuration;
    }
    

    function databaseExists($dbname) {
        $query = dibi::query("select schema_name from information_schema.schemata where schema_name = %s;", $dbname );

        if($query->fetchSingle()==0) {
            $query->free();
            return false;
        } else {
            $query->free();
            return true;
        }
    }

    function tablesExist($dbname) {
        $query = dibi::query("SELECT table_name
            FROM information_schema.tables
            WHERE table_schema = '$dbname'
            AND table_name = 'users'");
        if($query->fetchSingle()==0) {
            $query->free();
            return false;
        } else {
            $query->free();
            return true;
        }
    }

    function createLogin( $username ,$pass, $newUser = true) {
        $host = "localhost";
//        \Nette\Debug::dump("co mame na vstupu");
//        \Nette\Debug::dump($pass);
//        \Nette\Debug::dump($username);
        $password = PasswordHasher::databaseHash($username, $pass);
//        \Nette\Debug::dump("co mame na vystupu");
//        \Nette\Debug::dump($password);
//        die;
        if ($newUser) {
        
            dibi::nativeQuery("CREATE USER '$username'@'$host' IDENTIFIED BY '$password';");
            if ($username === AclConstants::ROOT_USER) {
                dibi::nativeQuery("GRANT ALL PRIVILEGES ON *.* TO '$username'@'$host' WITH GRANT OPTION;");
            }
            else {
                //TODO are all access rights necessary???
                dibi::nativeQuery("GRANT ALL PRIVILEGES ON *.* TO '$username'@'$host';");
            }
        } else {
            //TODO vyzkouset, jestli funguje
            dibi::nativeQuery("ALTER USER '$username'@'$host' WITH PASSWORD '$password';");
        }
    }

    function deleteLogin( $username ) {
        $host = "localhost";
        dibi::nativeQuery("DROP USER '$username'@'$host';");
    }

    
    
    
    protected static function getDatabase(array $configuration) {
        return $configuration["database"];
    }
    
    protected static function setMasterDatabase(array &$configuration) {
        unset($configuration["database"]);
    }
    
    protected static function setDatabase(array &$configuration, $database) {
        $configuration["database"] = $database;
    }
    
    protected static function setUser(array &$configuration, $user) {
        $configuration["username"] = $user;
    }
    
    protected static function setPassword(array &$configuration, $password) {
        $configuration["password"] = $password;
    }
}
